<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 资源输出 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace addons\index\controller;

use app\core\controller\CoreBaseController;

class ResourceController extends CoreBaseController {

    /**
     * 资源加载
     *
     * @param $moduleName
     * @return ResourceController
     */
    public function index($moduleName) {
        $path = $this->request->pathinfo();
        $resource = substr($path, strlen($moduleName . "/"));
        $contentType = $this->_detectContentType($resource);

        return $this->_resource($moduleName, $resource, $contentType);
    }

    /**
     * 检测资源文件对应的content type
     *
     * @param $resource
     * @return string
     */
    private function _detectContentType($resource) {
        if (endsWith($resource, ".css")) {
            return 'text/css';
        }
        if (endsWith($resource, ".js")) {
            return 'application/javascript';
        }
        if (endsWith($resource, ".png")) {
            return 'image/png';
        }
        if (endsWith($resource, ".jpg") || endsWith($resource, ".jpeg")) {
            return 'image/jpeg';
        }
        if (endsWith($resource, ".gif")) {
            return 'image/gif';
        }
        if (endsWith($resource, ".html") || endsWith($resource, ".htm")) {
            return 'text/html';
        }
        if (endsWith($resource, ".xml") || endsWith($resource, ".svg")) {
            return 'text/xml';
        }
        if (endsWith($resource, ".json")) {
            return 'application/json';
        }
        return 'text/plain';
    }

    /**
     * 模块logo
     *
     * @param $moduleName
     * @return string
     */
    public function logo($moduleName) {
        return $this->_resource($moduleName, "logo.png", 'image/png');
    }

    /**
     * javascript 资源文件
     *
     * @param $moduleName
     * @param $jsName
     * @return $this
     */
    public function js($moduleName, $jsName) {
        return $this->_resource($moduleName, "static/js/" . $jsName, 'application/javascript');
    }

    /**
     * css 资源文件
     *
     * @param $moduleName
     * @param $cssName
     * @return ResourceController
     */
    public function css($moduleName, $cssName) {
        return $this->_resource($moduleName, "static/css/" . $cssName, 'text/css');
    }

    /**
     * 图片资源文件
     *
     * @param $moduleName
     * @param $imgName
     * @return ResourceController
     */
    public function img($moduleName, $imgName) {
        $ext = substr(strrchr($imgName, '.'), 1);
        $contentType = 'image/' . $ext;
        return $this->_resource($moduleName, "static/img/" . $imgName, $contentType);
    }

    /**
     * 向页面输出资源文件
     *
     * @param $moduleName
     * @param $path
     * @param $contentType
     * @param int $maxAge
     * @return $this
     */
    protected function _resource($moduleName, $path, $contentType, $maxAge = 2592000) {
        $path = __DIR__ . "/../../" . $moduleName . "/" . $path;
        if (!file_exists($path)) {
            return response('', 404);
        }
        $size = filesize($path);
        if ($size > 0) {
            $data = fread(fopen($path, "r"), $size);
        } else {
            $data = "";
        }
        return response($data, 200, ['Content-Length' => $size])
            ->contentType($contentType)
            ->cacheControl("max-age=" . $maxAge)
            ->header("Expires: " . gmdate("D, d M Y H:i:s", (time() + $maxAge)) . " GMT")
            ->header("Last-Modified: " . gmdate("D, d M Y H:i:s", (filemtime($path))) . " GMT")
            ->header("Pragma: public");
    }
}